#Requires AutoHotkey v1.1
#Include XInput.ahk

XInput_Init()

Loop {
  Loop, 1 {
    if State := XInput_GetState(A_Index-1) {

            LT := State.bLeftTrigger
            RT := State.bRightTrigger
            wButtons := State.wButtons
            sThumbLX := State.sThumbLX
            sThumbLY := State.sThumbLY
            sThumbRX := State.sThumbRX
            sThumbRY := State.sThumbRY

            ToolTip, A_Index: %A_Index%`nbLeftTrigger: %LT%`nbRightTrigger: %RT%`nwButtons: %wButtons%`nsThumbLX: %sThumbLX%`nsThumbLY: %sThumbLY%`nsThumbRX: %sThumbRX%`nsThumbRY: %sThumbRY%

        ; Map buttons to keyboard keys
        if (State.wButtons & XINPUT_GAMEPAD_A)  ; A button
            Send, {A}
        if (State.wButtons & XINPUT_GAMEPAD_B)  ; B button
            Send, {B}
        if (State.wButtons & XINPUT_GAMEPAD_X)  ; X button
            Send, {X}
        if (State.wButtons & XINPUT_GAMEPAD_Y)  ; Y button
            Send, {Y}
        if (State.wButtons & XINPUT_GAMEPAD_DPAD_UP)
            Send, {Up}
        if (State.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
            Send, {Down}
        if (State.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
            Send, {Left}
        if (State.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
            Send, {Right}
        if (State.wButtons & XINPUT_GAMEPAD_START)
            Send, {Enter}
        if (State.wButtons & XINPUT_GAMEPAD_BACK)
            Send, {Esc}
        if (State.wButtons & XINPUT_GAMEPAD_LEFT_THUMB)
            Click, Left
        if (State.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB)
            Click, Right
        if (State.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER)
            Send, {PgUp}
        if (State.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER)
            Send, {PgDn}
        if (State.wButtons & XINPUT_GAMEPAD_GUIDE)
            Send, {LWin}

        ; Map left stick to mouse movement
        if (State.sThumbLX > 10000 or State.sThumbLX < -10000 or State.sThumbLY > 10000 or State.sThumbLY < -10000)
        {
            MouseMove, State.sThumbLX / 500, -State.sThumbLY / 500, 0, R
        }
    }
    Sleep, 100
  }
}

XInput_Term()
return

